<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$url = MiscUtils::getParam('url', NULL);
$page_id = MiscUtils::getParam('i', NULL);
$inst_id = MiscUtils::getParam('inid', NULL);
$folder = MiscUtils::getParam('infold', NULL);

$myPdo = DbUtils::createPdoInst();
$data = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'user_profile', NULL, NULL, NULL, NULL, NULL, NULL)->d[0];

?>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  outline: 0;
  margin: 0;
}
body {
  background-image: url(assets/imgs/bg.png);
  background-repeat: repeat;
}
.form {
  background-color: #fff;
}
.form td {
  vertical-align: middle;
  padding: 2px 5px;
}
.form th.header {
  text-align: left;
  color: #fff;
  background-color: #e09696;
  padding-left: 5px;
  font-weight: bold;
  height: 25px;
}
.form td.title {
  background-color: #f1d0d0;
  border-top: 1px solid #fff;
  width: 150px;
}
.form td.content {
  width: 200px;
  padding: 0;
}
.form td.content input {
  width: 99%;
  border: solid 1px #ddd;
}

.image_button {
  background-color: #fff;
  border-bottom: 2px solid #666666;
  border-right: 2px solid #666666;
  border-top: 1px solid #bbbbbb;
  border-left: 1px solid #bbbbbb;
  font-weight: bold;
  cursor: pointer;
  display: inline-block;
  width: 60px;
  height: 25px;
  line-height: 25px;
  text-align: center;
  vertical-align: middle;
  color: #000;
}
</style>
<table class="form" cellPadding=0 cellSpacing=0>
  <tr><th class="header" colspan=2>Account</th></tr>
  <tr>
    <td class="title">Account</td>
    <td class="content"><input type="text" id="account" value="<?php echo $data->account; ?>"></input></td>
  </tr>
  <tr>
    <td class="title">Password</td>
    <td class="content"><input type="password" id="password" value=""></input></td>
  </tr>
</table>
<span style="margin-top: 5px;" class="image_button" onclick="check_save();">Save</span>

<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../incls/js/checkutils.js"></script>
<script type="text/javascript">
  var _checks_add = [];
  var _errors_add = [];
  
  function check_save() {
    CheckUtils.addToArray(_checks_add, $('#account')[0], check_account, _errors_add);
    CheckUtils.addToArray(_checks_add, $('#password')[0], check_password, _errors_add);
    save();
  }

  function save() {
    if (_checks_add.length > 0) {
      setTimeout('save();', 500);
      return;
    }
    if (_errors_add.length > 0) {
      return;
    }
    var obj = {
      <?php if (isset($data->id)) { echo 'id:'. $data->id . ','; } ?>
      account: $('#account')[0].value,
      _password: $('#password')[0].value
    };
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=save&q=' + encodeURIComponent(JSON.stringify(obj)),
      success: function(msg){
        try {
          JSON.parse(msg);
          location.reload();
        } catch (ex) {
          alert( 'Error: ' + ex );
        }
      }
    });
  }

  function check_account(inputElm, callbackFunc, errorArr) {
    if (inputElm.value == '') {
      CheckUtils.addError(inputElm, errorArr, 'Required');
      callbackFunc();
      return;
    }
    callbackFunc();
  }

  function check_password(inputElm, callbackFunc, errorArr) {
    if (inputElm.value != '' && inputElm.value.length < 6){
      CheckUtils.addError(inputElm, errorArr, 'At least 6');
    }
    callbackFunc();
  }
</script>